
**********Employment***********
use "$raw\Block-6-7-Employment particulars and Compensation to workers-records.dta", clear
// gen  obsId = Key_entpr
keep if B6_7_c1=="619"
drop B6_7_c1
rename  B6_7_c3 L
keep Key_entpr L
gen ln_L=ln(L)
save "$data\SUM_Firm_level_L.dta", replace

***ASICC***
use "$raw\Block-2-Operation and background information-records.dta", clear
* Selecting basic sample which drops nics outside 15 to 36
gen nic04_4d = real(substr(trim(B2_q201),1,4))
gen nic04_2d = floor(nic04_4d/100)
keep if nic04_2d>=15&nic04_2d<=36
rename key_entpr Key_entpr
keep Key_entpr nic04_4d nic04_2d Sector State_region state nss nsc Wgt
* Dropping Sikkim, Arunachal Pradesh, Mizoram, and Lakshadweep as these were missing in the ASI 2005
drop if state=="11"|state=="12"|state=="15"|state=="31"
// rename FSU_Key_variable FSU
merge 1:1 Key_entpr using "$data\SUM_Firm_level_L.dta"
keep if _merge==3
drop _merge
save "$data\Firm_level.dta", replace

************Inputs************

use "$raw\Block-3-Principal operating expenses-records.dta", clear
**Many firms are missing asicc
destring B3_c1, replace
*Dropping all obs that dont have a product code
drop if B3_c1>305
drop if missing(B3_c3)
gen asicc=real(B3_c3)
rename B3_c4 productUnit
rename B3_c5 inputQuantity
rename B3_c6 inputValue
destring productUnit, replace

sort asicc 
merge m:1 asicc using "$concordances\ASICC_05_06_list.dta"
keep if _merge==3
drop _merge
 
merge m:1 productUnit using "$concordances\sum_productunits_conc.dta"
drop if _merge~=3
drop _merge
rename productUnit inputUnit
ren productUnit_con inputUnit_con
ren productQty_multfactor inputQty_multfactor

rename key_entpr Key_entpr
merge m:1 Key_entpr using "$data\Firm_level.dta"
keep if _merge==3
drop _merge


* Generating Prices
destring  inputValue, replace
gen input_unit_cat = asicc*100 +  inputUnit_con
gen  inputQuantity_con =  inputQuantity* inputQty_multfactor
gen price = inputValue/inputQuantity_con
gen ln_price = log(price)
drop if ln_L==.

gen unit_ok = 0
replace unit_ok = 1 if inputUnit_con== unit_red_ASI_NSS_05

xtset input_unit_cat
xtreg ln_price ln_L if ln_price~=., fe r
gen survey="SUM"
destring state, replace
destring Sector, replace
save "$data\sum2005_inputlevel04082013.dta", replace


***********Outputs*************
use "$raw\Block-4-Principal receipts-records.dta", clear

destring B4_c1, replace
keep if B4_c1<406
rename B4_c3 asicc
destring asicc, replace
rename B4_c6 productValue 
rename B4_c5 productQuantity
rename B4_c4 productUnit
destring productUnit, replace

merge m:1 productUnit using "$concordances\sum_productunits_conc.dta"
drop if _merge~=3
drop _merge

merge m:1 asicc using "$concordances\ASICC_05_06_list.dta"
drop if _merge~=3
drop _merge


gen product_unit_cat = asicc*100 +  productUnit_con
gen  productQuantity_con =  productQuantity* productQty_multfactor
gen price = productValue/productQuantity_con
gen ln_price = log(price)
rename key_ientpr Key_entpr
merge m:1 Key_entpr using "$data\Firm_level.dta"
keep if _merge==3
drop _merge


xtset product_unit_cat
xtreg ln_price ln_L, fe r

gen survey="SUM"
destring Sector, replace
destring state, replace
save "$data\sum2005_productlevel04082013.dta", replace
//
// ^
// // append using "$raw\Block-4dot1-5-Other receipts and gross value added-records.dta"
//
// use "$raw\Block-1-identification of Sample Enterprise-Establishment-reords.dta", clear
// **Keeping total employment**
// // keep if S_no==10
// // rename avg_pers_workd L
// // keep DSL L
// // save "$data\Firm_level_L.dta"
